﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.StandardMixForms
{
    public partial class StandardMixDetailForm : DevExpress.XtraEditors.XtraForm
    {
        int _option;
        BLL.ToolBll toolBll = new BLL.ToolBll();
        BLL.StandardMixBll standardMixBll = new BLL.StandardMixBll();
        StandardMix _standardMix;
        Action _InitGrid;
        public StandardMixDetailForm(Action InitGrid)
        {
            InitializeComponent();
            _option = 1;
            _InitGrid = InitGrid;
        }
        public StandardMixDetailForm(StandardMix standardMix)
        {
            InitializeComponent();
            _option = 2;
            _standardMix = standardMix;
        }

        private void StandardMixDetailForm_Load(object sender, EventArgs e)
        {
            if (_option == 1)
            {
                //绑定样品名称下拉框
                this.searchLookUpEdit_goodsName.Properties.DataSource = toolBll.GetSemiGoodList();
                //绑定 检验项目下拉框
                this.searchLookUpEdit_TestItemName.Properties.DataSource = toolBll.GetTestItemList();
            }
            if (_option == 2)
            {
                //绑定样品名称下拉框
                this.searchLookUpEdit_goodsName.Properties.DataSource = toolBll.GetAllGoodList();
                //绑定 检验项目下拉框
                this.searchLookUpEdit_TestItemName.Properties.DataSource = toolBll.GetAllTestItemList();
                searchLookUpEdit_goodsName.Enabled = false;
                searchLookUpEdit_TestItemName.Enabled = false;
                this.searchLookUpEdit_goodsName.EditValue = _standardMix.GoodsID;
                this.searchLookUpEdit_TestItemName.EditValue = _standardMix.TestItemID;

                this.textEdit_MaxValue.Text = _standardMix.MaxValue.ToString();
                this.textEdit_minValue.Text = _standardMix.MinValue.ToString();
                this.textEdit_Order.Text = _standardMix.FormOrder.ToString();
                this.textEdit_Remark.Text = _standardMix.Remark;
            }
        }

        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            //判空
            if (this.searchLookUpEdit_goodsName.EditValue == null || this.searchLookUpEdit_goodsName.EditValue.ToString() == "")
            {
                this.searchLookUpEdit_goodsName.Focus();
                XtraMessageBox.Show("样品名称必选！");
                return;
            }
            if (this.searchLookUpEdit_TestItemName.EditValue == null || this.searchLookUpEdit_TestItemName.EditValue.ToString() == "")
            {
                this.searchLookUpEdit_TestItemName.Focus();
                XtraMessageBox.Show("检验项目必选！");
                return;
            }
            if (this.textEdit_Order.Text == "")
            {
                this.textEdit_Order.Focus();
                XtraMessageBox.Show("顺序必填！");
                return;
            }

            if (_option == 1)
            {
                StandardMix standardMix = new StandardMix();
                standardMix.GoodsID = Convert.ToString(this.searchLookUpEdit_goodsName.EditValue);
                standardMix.TestItemID = Convert.ToString(this.searchLookUpEdit_TestItemName.EditValue);
                standardMix.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                if (this.textEdit_minValue.Text != "")
                {
                    Convert.ToDouble(this.textEdit_minValue.Text);
                    standardMix.MinValue =this.textEdit_minValue.Text;
                }
                else
                {
                    standardMix.MinValue = null;
                }
                if (this.textEdit_MaxValue.Text != "")
                {
                    Convert.ToDouble(this.textEdit_MaxValue.Text);
                    standardMix.MaxValue = this.textEdit_MaxValue.Text;
                }
                else
                {
                    standardMix.MaxValue = null;
                }
                standardMix.Remark = this.textEdit_Remark.Text;

                if (standardMix.MinValue!=null && standardMix.MaxValue!=null)
                {
                    if (Convert.ToDouble(standardMix.MinValue)>Convert.ToDouble(standardMix.MaxValue))
                    {
                        XtraMessageBox.Show("最小值不能大于最大值");
                        return;
                    }
                }

                StandardMix res = standardMixBll.AddEntity(standardMix);
                if (res != null)
                {
                    _InitGrid();
                    XtraMessageBox.Show("添加成功！");
                }

            }
            if (_option == 2)
            {
                _standardMix.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                if (this.textEdit_minValue.Text != "")
                {
                    Convert.ToDouble(this.textEdit_minValue.Text);
                    _standardMix.MinValue = this.textEdit_minValue.Text;
                }
                else
                {
                    _standardMix.MinValue = null;
                }
                if (this.textEdit_MaxValue.Text != "")
                {
                    Convert.ToDouble(this.textEdit_MaxValue.Text);
                    _standardMix.MaxValue = this.textEdit_MaxValue.Text;
                }
                else
                {
                    _standardMix.MaxValue = null;
                }
                _standardMix.Remark = this.textEdit_Remark.Text;

                if (_standardMix.MinValue != null && _standardMix.MaxValue != null)
                {
                    if ( Convert.ToDouble(_standardMix.MinValue) >  Convert.ToDouble(_standardMix.MaxValue))
                    {
                        XtraMessageBox.Show("最小值不能大于最大值");
                        return;
                    }
                }
                bool res = standardMixBll.EditEntity(_standardMix);
                if (res)
                {
                    XtraMessageBox.Show("修改成功");
                }
            }
        }
    }
}